//
//  TJEmptyDataHandler.m
//  空视图封装
//
//  Created by wang tiao on 2020/12/19.
//

#import "TJEmptyDataHandler.h"
#import "UIScrollView+EmptyDataSet.h"

@implementation TJEmptyDataModel
 

@end


@implementation TJEmptyDataView {
    UIImageView *_iconImageView;
    UILabel *_textLabel;
    UILabel *_detailLabel;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        _emptyDataType = TJEmptyDataTypeBase;
        
        [self setupUI];
        [self layout];
        
        TJEmptyDataModel *emptyDataModel = [[TJEmptyDataModel alloc] init];
        emptyDataModel.iconName = @"no_data_icon";
        emptyDataModel.text = @"Empty data";
        self.emptyDataModel = emptyDataModel;
    }
    return self;
}

- (void)setEmptyDataType:(TJEmptyDataType)emptyDataType {
    _emptyDataType = emptyDataType;
    [self layout];
}

- (void)setEmptyDataModel:(TJEmptyDataModel *)emptyDataModel {
    _emptyDataModel = emptyDataModel;
    
    if (_emptyDataType == TJEmptyDataTypeOnlyText && emptyDataModel.text.length == 0) {
        emptyDataModel.text = @"No data~";
    }

    _detailLabel.text = emptyDataModel.detailText;
    _iconImageView.hidden = _emptyDataType == TJEmptyDataTypeOnlyText;
    _textLabel.text = emptyDataModel.text;
    _iconImageView.image = [UIImage imageNamed:emptyDataModel.iconName];

    if (_emptyDataModel.textColor) _textLabel.textColor = emptyDataModel.textColor;
    if (_emptyDataModel.detailAttributedString) _detailLabel.attributedText = emptyDataModel.detailAttributedString;
}

- (void)setupUI {
    
    _iconImageView = [UIImageView addToView:self];
    _textLabel = [UILabel lableWithSuperView:self font:UIFontMake(14) color:UIColorMake(0x4F5561) title:@"" textAlignment:NSTextAlignmentLeft];
    _detailLabel = [UILabel lableWithSuperView:self font:UIFontMake(13) color:UIColor.tj_9B9B9BColor title:nil textAlignment:NSTextAlignmentLeft];
    _detailLabel.numberOfLines = 0;
}

- (void)layout {
    
    [_iconImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.offset(0);
    }];
    
    [_textLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.bottom.equalTo(_iconImageView.mas_bottom).offset(12);
    }];
    
    if (_emptyDataType == TJEmptyDataTypeOnlyText) {
        
        [_iconImageView mas_remakeConstraints:^(MASConstraintMaker *make) {}];
        
        [_textLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.offset(0);
            make.top.offset(0);
        }];
        return;
    }
    
    _lastView = _textLabel;
    
    if (_emptyDataType == TJEmptyDataTypeDetail) {
        [_detailLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.offset(0);
            make.top.equalTo(_textLabel.mas_bottom).offset(26);
        }];
        _lastView = _detailLabel;
        return;
    }
    
 
}

@end



@interface TJEmptyDataHandler()<DZNEmptyDataSetDelegate,DZNEmptyDataSetSource>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) TJEmptyDataView *emptyDataView;
@property (nonatomic, strong) NSLayoutConstraint *heightConstraint;
@property (nonatomic, assign) TJEmptyDataType type;
@property (nonatomic, assign) TJEmptyDataType originalType;
@property (nonatomic, strong) TJEmptyDataModel *dataModel;


@end

@implementation TJEmptyDataHandler


+ (instancetype)emptyDataHandleWithScrollView:(UIScrollView *)scrollView configuration:(TJEmptyDataType (^_Nullable)(TJEmptyDataModel *dataModel))configuration {
    TJEmptyDataHandler *emptyDataManager = [[TJEmptyDataHandler alloc] init];
    
#ifdef APP_EXTENSION
    emptyDataManager.offsetY = 0;
#else
    emptyDataManager.offsetY = -KNavBarHight * 0.5;
#endif
    
    emptyDataManager.addOffsetY = -20;
    emptyDataManager.backgroundColor = scrollView.backgroundColor;
    emptyDataManager.scrollView = scrollView;
    scrollView.emptyDataSetDelegate = emptyDataManager;
    scrollView.emptyDataSetSource = emptyDataManager;
    [emptyDataManager.emptyDataView.loadButton addTarget:emptyDataManager action:@selector(didClickLoadButton) forControlEvents:UIControlEventTouchUpInside];
    if (configuration) {
        TJEmptyDataType dataType = configuration(emptyDataManager.emptyDataView.emptyDataModel);
        emptyDataManager.type = dataType;
        emptyDataManager.originalType = dataType;
        emptyDataManager.dataModel = emptyDataManager.emptyDataView.emptyDataModel;
    }
    return emptyDataManager;
}

+ (instancetype)emptyDataHandleWithScrollView:(UIScrollView *)scrollView {
    return [self emptyDataHandleWithScrollView:scrollView configuration:nil];
}

- (void)reloadEmptyDataSet {
    [self.scrollView reloadEmptyDataSet];
}

- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    return _canDisplay;
}

- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView {
    return _backgroundColor;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView {
    return YES;
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    
    if (_ignoreOffset) {
        return _offsetY;
    }
    
    UIEdgeInsets contInset =  self.scrollView.contentInset;
    if ([_scrollView isKindOfClass:UITableView.class]) {
        UITableView *tableView = (UITableView *)_scrollView;
        return _offsetY + tableView.tableHeaderView.tj_size.height * 0.5 + contInset.top + _addOffsetY;
    }
    return _offsetY;
}

- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {
    return self.emptyDataView;
}

- (void)emptyDataSetDidAppear:(UIScrollView *)scrollView {
    
    [self.emptyDataView.superview setNeedsLayout];
    [self.emptyDataView.superview layoutIfNeeded];
    
    CGFloat showHight = CGRectGetMaxY(self.emptyDataView.lastView.frame);
    [_emptyDataView removeConstraint:_heightConstraint];
    _heightConstraint = [NSLayoutConstraint constraintWithItem:_emptyDataView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:showHight];
    [_emptyDataView addConstraint:_heightConstraint];
}

- (TJEmptyDataView *)emptyDataView {
    if (!_emptyDataView) {
        _emptyDataView = [[TJEmptyDataView alloc] init];
        _heightConstraint = [NSLayoutConstraint constraintWithItem:_emptyDataView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:500];
        [_emptyDataView addConstraint:_heightConstraint];
    }
    return _emptyDataView;
}

- (void)setType:(TJEmptyDataType)type {
    _type = type;
    self.emptyDataView.emptyDataType = type;
}

- (void)didClickLoadButton {
    if (_didClickLoadButtonBlock) {
        _didClickLoadButtonBlock();
    }
}
- (void)setDataModel:(TJEmptyDataModel *)dataModel {
    _dataModel = dataModel;
    self.emptyDataView.emptyDataModel = dataModel;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


@end
